package com.wangzeqi.neihanduanzi.home.essence;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.wangzeqi.neihanduanzi.R;
import com.wangzeqi.neihanduanzi.web.WebViewActivity;
import com.wangzeqi.neihanduanzi.cache.AllLruCacheLoad;
import com.wangzeqi.neihanduanzi.home.essence.entity.EssenceEntity;

/**
 * Created by dllo on 16/1/18.
 */
public class AdapterEssence extends RecyclerView.Adapter {

    private EssenceEntity entity;
    private Context context;
    private RequestQueue queue;

    public AdapterEssence( Context context) {
        this.context = context;

    }
    public  void  addData(EssenceEntity entity){
        this.entity = entity;
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType ==1){
            View view = LayoutInflater.from(context).inflate(R.layout.essence_head,null);
            EssenceViewHolderHead viewHolderHead = new EssenceViewHolderHead(view);
            return viewHolderHead;
        }else {
            View view = LayoutInflater.from(context).inflate(R.layout.essence_item, null);
            EssenceViewHolder viewHolder = new EssenceViewHolder(view);
            return viewHolder;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        queue = Volley.newRequestQueue(context);
        AllLruCacheLoad cache = new AllLruCacheLoad();
        final ImageLoader imageloader = new ImageLoader(queue, cache);
        if (getItemViewType(position) == 1 ) {
            EssenceViewHolderHead viewHolderHead = (EssenceViewHolderHead) holder;
            viewHolderHead.imageView.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
            viewHolderHead.imageView.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
            viewHolderHead.imageView.setImageUrl(entity.getData().get(0).getImage_list().get(0).getUrl(), imageloader);
            viewHolderHead.textViewTitle.setText(entity.getData().get(0).getTitle());
            viewHolderHead.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context,WebViewActivity.class);
                    intent.putExtra("url",entity.getData().get(0).getSource_url());
                    context.startActivity(intent);
                }
            });
        }
        else {
            String data =entity.getData().get(position).getDatetime();
            String dt = data.substring(data.indexOf("-")+1,data.indexOf(" "));
            EssenceViewHolder viewHolder = (EssenceViewHolder) holder;
            viewHolder.imageView.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
            viewHolder.imageView.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
            viewHolder.imageView.setImageUrl(entity.getData().get(position).getImage_list().get(0).getUrl(), imageloader);
            viewHolder.textViewDate.setText(dt);
            viewHolder.textViewContent.setText(entity.getData().get(position).getTitle());
            viewHolder.textViewContent.setText(entity.getData().get(position).getTitle());
            viewHolder.relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context,WebViewActivity.class);
                    intent.putExtra("url",entity.getData().get(position).getSource_url());
                    context.startActivity(intent);
                }
            });
        }
    }


    @Override
    public int getItemCount() {
        if (entity != null) {
            return entity.getData().size();
        }
        return 0;
    }
    @Override
    public int getItemViewType(int position) {
        if (position == 0){
            return 1;
        }else {
        return 2;

        }
    }

    class EssenceViewHolder extends RecyclerView.ViewHolder{
        NetworkImageView imageView;
        TextView textViewContent,textViewDate;
        RelativeLayout relativeLayout;
        public EssenceViewHolder(View itemView) {
            super(itemView);
            imageView = (NetworkImageView) itemView.findViewById(R.id.essence_picture);
            textViewContent = (TextView) itemView.findViewById(R.id.essence_content);
            textViewDate = (TextView) itemView.findViewById(R.id.essence_date);
            relativeLayout = (RelativeLayout) itemView.findViewById(R.id.essence_home);
        }
    }
    class  EssenceViewHolderHead extends RecyclerView.ViewHolder{

        NetworkImageView imageView;
        TextView textViewTitle;

        public EssenceViewHolderHead(View itemView) {
            super(itemView);
            imageView = (NetworkImageView) itemView.findViewById(R.id.essence_head_picture);
            textViewTitle = (TextView) itemView.findViewById(R.id.essence_head_tx);

        }
    }
}
